#include <unistd.h>
#include "ohos_init.h"
#include "los_task.h"
#include "cmsis_os2.h"
#include "securec.h"
/* 任务ID */
static osThreadId_t g_testTaskId01;
static osThreadId_t g_testTaskId02;

/* 测试任务优先级 */
#define TASK_PRIO_TEST osPriorityNormal
/* 信号量结构体id */
static osSemaphoreId_t g_semId;
void Example_SemTask1(void *arg)
{
    (void)arg;
    osStatus_t ret;
    printf("Example_SemTask1 try get sem g_semId ,timeout 10 ticks.\n");
    /* 定时阻塞模式申请信号量，定时时间为10ticks */
    ret = osSemaphoreAcquire(g_semId, 10);
    /* 申请到信号量 */
    if (ret == osOK) {
        osSemaphoreRelease(g_semId);
        return;
    }
    /* 定时时间到，未申请到信号量 */
    if (ret == osErrorTimeout) {
        printf("Example_SemTask1 timeout and try get semg_semId wait forever.\n");
        /*永久阻塞模式申请信号量*/
        ret = osSemaphoreAcquire(g_semId, osWaitForever);
        printf("Example_SemTask1 wait_forever and get semg_semId .\n");
        if (ret == osOK) {
            osSemaphoreRelease(g_semId);
            osThreadSuspend(g_testTaskId01);
            return;
        }
    }
}

void Example_SemTask2(void *arg)
{
    (void)arg;
    osStatus_t ret;
    printf("Example_SemTask2 try get sem g_semId wait forever.\n");
    /* 永久阻塞模式申请信号量 */
    ret = osSemaphoreAcquire(g_semId, osWaitForever);
    if (ret == osOK) {
        printf("Example_SemTask2 get sem g_semId and then delay 20ticks .\n");
    }
    /* 任务休眠20 ticks */
    osDelay(20);
    printf("Example_SemTask2 post sem g_semId .\n");
    /* 释放信号量 */
    osSemaphoreRelease(g_semId);
    osThreadSuspend(g_testTaskId02);
    return;
}

void ExampleTaskEntry(void)
{
    osStatus_t ret;
    /* 创建信号量 */
    g_semId = osSemaphoreNew(0xFFFF,0,NULL);
    /* 锁任务调度 */
    osKernelLock();
    /* 创建任务1 */
    osThreadAttr_t task1_attr = {
        .name = "task1",
        .stack_size = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE,
        .stack_mem = NULL,
        .priority = TASK_PRIO_TEST,
    };
    g_testTaskId01=osThreadNew((osThreadFunc_t)Example_SemTask1,NULL,&task1_attr);
    if (g_testTaskId01 == NULL) {
        printf("task1 create failed .\n");
        return osError;
    }
    /* 创建任务2 */
    osThreadAttr_t task2_attr = {
        .name = "task2",
        .stack_size = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE,
        .stack_mem = NULL,
        .priority = (TASK_PRIO_TEST+1),
    };
    g_testTaskId02=osThreadNew((osThreadFunc_t)Example_SemTask2,NULL,&task2_attr);
    if (g_testTaskId02 == NULL) {
        printf("task2 create failed .\n");
        return osError;
    }
    /* 解锁任务调度 */
    osKernelUnlock();
    osSemaphoreRelease(g_semId);
    /* 任务休眠40 ticks */
    osDelay(40);
    /* 删除信号量 */
    osSemaphoreDelete(g_semId);
    /* 删除任务1 */
    ret = osThreadTerminate(g_testTaskId01);
    if (ret != osOK) {
        printf("task1 delete failed .\n");
        return osError;
    }else{
        printf("task1 delete success \n");
    }
    /* 删除任务2 */
    ret = osThreadTerminate(g_testTaskId02);
    if (ret != osOK) {
        printf("task2 delete failed .\n");
        return osError;
    }else{
        printf("task2 delete success \n");
    }
    return osError;
}

APP_FEATURE_INIT(ExampleTaskEntry);